/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.dialect.editor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.qdss.persist.dialect.FieldType;


/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Feb 14, 2009
 * @version $Id: TimestampEditor.java 52 2009-05-25 09:54:02Z zhaofang123@gmail.com $
 */
public class TimestampEditor extends DateTimeEditor {

	@Override
	public int getType() {
		return FieldType.TIMESTAMP.getMask();
	}

	@Override
	public void set(PreparedStatement pstmt, int index, Object value)
			throws SQLException {
		Timestamp v = null;
		if (value.getClass() == java.util.Date.class)
			v = new Timestamp( ((java.util.Date) value).getTime() );
		else
			v = (Timestamp) value;
		pstmt.setTimestamp(index, v);
	}
	
	@Override
	public Object get(ResultSet rs, int index) throws SQLException {
		Timestamp v = rs.getTimestamp(index);
		return (v == null) ? null : new Date(v.getTime());
	}
}
